package cn.innosmart.aq.bean;


import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.innosmart.aq.util.DeviceConstant;
import cn.innosmart.aq.util.SystemConstant;

/**
 * Created by David on 2015/7/23.
 */
public class AquariumBean implements Parcelable {
    public static final Parcelable.Creator<AquariumBean> CREATOR = new Parcelable.Creator<AquariumBean>() {
        public AquariumBean createFromParcel(Parcel source) {
            return new AquariumBean(source);
        }

        public AquariumBean[] newArray(int size) {
            return new AquariumBean[size];
        }
    };
    //鱼缸的ID
    private String id;
    //鱼缸的标题
    private String title;
    //鱼缸的序列号
    private int order;
    //鱼缸的设备列表
    private ArrayList<String> deivices;
    //鱼缸的摄像肉列表
    private ArrayList<String> cameras = new ArrayList<>();
    //鱼缸的ICON类型
    private String icon_type;
    //鱼缸的ICON数据
    private String icon_data;
    //鱼缸的报警状态
    private boolean isAlarm;
    //鱼缸的温度类型设备
    private ArrayList<String> temperatureDevices;
    //鱼缸的PH类型设备
    private ArrayList<String> phDevices;
    //鱼缸的水位类型设备
    private ArrayList<String> waterLeverDevices;
    //鱼缸的其他类型设备
    private ArrayList<String> otherDevices;
    //鱼缸的插座类型设备
    private ArrayList<String> switchDevices;
    private DeviceBean waterLever;
    //鱼缸的可执行设备
    private ArrayList<String> actionDevices;
    //鱼缸的规则列表
    private ArrayList<RuleBean> ruleList = null;
    //鱼缸的场景列表
    private ArrayList<SceneBean> sceneList = null;
    //鱼缸的排程列表
    private ArrayList<ScheduleBean> scheduleList = null;

    private int maintainMode = 0;

    public AquariumBean(JSONObject jsonObject) {
        try {
            this.id = jsonObject.getString("id");
            this.title = jsonObject.getString("title");
            this.order = jsonObject.getInt("order");
            this.icon_type = jsonObject.getString("icon_type");
            this.icon_data = jsonObject.getString("icon_data");
            this.deivices = new ArrayList<String>();
            JSONArray addressArray = jsonObject.getJSONArray("addresses");
            for (int i = 0; i < addressArray.length(); i++) {
                deivices.add((String) addressArray.get(i));
            }
            this.cameras = new ArrayList<String>();
            try {
                JSONArray jsCam = jsonObject.getJSONArray("ipcams");
                for (int i = 0; i < jsCam.length(); i++) {
                    cameras.add((String) jsCam.get(i));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            this.maintainMode = jsonObject.getInt("maintain");
            System.out.println("Place " + this.title + " maintain:" + this.maintainMode);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public AquariumBean() {
    }

    protected AquariumBean(Parcel in) {
        this.id = in.readString();
        this.title = in.readString();
        this.order = in.readInt();
        this.deivices = in.createStringArrayList();
        this.icon_type = in.readString();
        this.icon_data = in.readString();
        this.isAlarm = in.readByte() != 0;
        this.temperatureDevices = in.createStringArrayList();
        this.phDevices = in.createStringArrayList();
        this.waterLeverDevices = in.createStringArrayList();
        this.otherDevices = in.createStringArrayList();
        this.switchDevices = in.createStringArrayList();
        this.waterLever = in.readParcelable(DeviceBean.class.getClassLoader());
        this.actionDevices = in.createStringArrayList();
        this.ruleList = new ArrayList<RuleBean>();
        in.readList(this.ruleList, List.class.getClassLoader());
        this.sceneList = in.createTypedArrayList(SceneBean.CREATOR);
        this.scheduleList = new ArrayList<ScheduleBean>();
        in.readList(this.scheduleList, List.class.getClassLoader());
        this.maintainMode = in.readInt();
    }

    public static boolean checkAquariumNameExist(String name) {
        boolean isExist = false;
        if (SystemConstant.aquariumList == null) {
            SystemConstant.aquariumList = new ArrayList<AquariumBean>();
        }
        for (int i = 0; i < SystemConstant.aquariumList.size(); i++) {
            if (name.equals(SystemConstant.aquariumList.get(i).getTitle())) {
                isExist = true;
                break;
            }
        }
        return isExist;
    }

    /**
     * 通过ID找相应的鱼缸
     *
     * @param aquariumBeanId
     * @return
     */
    public static AquariumBean findAquariumById(String aquariumBeanId) {
        AquariumBean aquarium = null;
        if (SystemConstant.aquariumList == null) {
            return null;
        }
        for (AquariumBean aquariumBean : SystemConstant.aquariumList) {
            if (aquariumBeanId.equals(aquariumBean.getId())) {
                aquarium = aquariumBean;
            }
        }
        return aquarium;
    }

    public ArrayList<String> getCameras() {
        return cameras;
    }

    public void setCameras(ArrayList<String> cameras) {
        this.cameras = cameras;
    }

    public boolean isAlarm() {
        return isAlarm;
    }

    public void setIsAlarm(boolean isAlarm) {
        this.isAlarm = isAlarm;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public ArrayList<String> getDeivices() {
        return deivices;
    }

    public void setDeivices(ArrayList<String> deivices) {
        this.deivices = deivices;
    }

    public String getIcon_type() {
        return icon_type;
    }

    public void setIcon_type(String icon_type) {
        this.icon_type = icon_type;
    }

    public String getIcon_data() {
        return icon_data;
    }

    public void setIcon_data(String icon_data) {
        this.icon_data = icon_data;
    }

    public int getMaintainMode() { return this.maintainMode; };

    public void setMaintainMode(int status) { this.maintainMode = status; };


    @Override
    public String toString() {
        return "AquariumBean{" +
                "id='" + id + '\'' +
                ", title='" + title + '\'' +
                ", order=" + order +
                ", deivices=" + deivices +
                ", icon_type='" + icon_type + '\'' +
                ", icon_data='" + icon_data + '\'' +
                '}';
    }

    //获得温度值显示在简要列表上
    public String getTemperatureValue() {
        if (deivices.size() == 0) {
            return null;
        } else {
            if (temperatureDevices == null) {
                getTemperatures();
            }
            //取第一个设备的值
            if (temperatureDevices.size() != 0) {
                DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(temperatureDevices.get(0));
                if (deviceBean != null) {
                    int sensorunity = deviceBean.getSensorunit();
                    double value = deviceBean.getSensorvalue();
                    return DeviceConstant.caculateValue(sensorunity, value);
                }
            }
        }
        return null;
    }


    //获得PH值显示在简要列表上
    public String getPhValue() {
        if (deivices.size() == 0) {
            return null;
        } else {
            if (phDevices == null) {
                getPhs();
            }
            if (phDevices.size() != 0) {
                //取第一个设备的值
                DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(phDevices.get(0));
                if (deviceBean != null) {
                    int sensorunity = deviceBean.getSensorunit();
                    double value = deviceBean.getSensorvalue();
                    return DeviceConstant.caculateValue(sensorunity, value);
                }
            }
        }
        return null;
    }

    //获得水位状态显示在简要列表上
    public String getWaterLeverValue(Context context) {
        if (deivices.size() == 0) {
            return null;
        } else {
            if (!isWaterLeverExist()) {
                return null;
            } else {
                //取第一个设备的值
                if (waterLever != null) {
                    double value = waterLever.getSensorvalue();
                    return DeviceConstant.caculateWaterLever(context, value);
                }
            }
        }
        return null;
    }

    /**
     * 获得温度列表
     *
     * @return
     */
    public ArrayList<String> getTemperatures() {
        if (temperatureDevices == null) {
            temperatureDevices = new ArrayList<String>();
        } else {
            temperatureDevices.clear();
        }
        for (int i = 0; i < deivices.size(); i++) {
            DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(deivices.get(i));
            int functype = deviceBean.getFunctype();
            if (functype == DeviceConstant.FUNCTYPE_TEMPERATURE) {
                temperatureDevices.add(deivices.get(i));
            }
        }
        return temperatureDevices;
    }

    /**
     * 获得PH列表
     *
     * @return
     */
    public ArrayList<String> getPhs() {
        if (phDevices == null) {
            phDevices = new ArrayList<String>();
        } else {
            phDevices.clear();
        }
        for (int i = 0; i < deivices.size(); i++) {
            int functype = SystemConstant.deviceBeanHashMap.get(deivices.get(i)).getFunctype();
            if (functype == DeviceConstant.FUNCTYPE_PH) {
                phDevices.add(deivices.get(i));
            }
        }
        return phDevices;
    }

    /**
     * 获得水位列表
     *
     * @return
     */
    public ArrayList<String> getWaterLevers() {
        if (waterLeverDevices == null) {
            waterLeverDevices = new ArrayList<String>();
        } else {
            waterLeverDevices.clear();
        }
        for (int i = 0; i < deivices.size(); i++) {
            int functype = SystemConstant.deviceBeanHashMap.get(deivices.get(i)).getFunctype();
            if (functype == DeviceConstant.FUNCTYPE_WATERLEVER) {
                waterLeverDevices.add(deivices.get(i));
            }
        }
        return waterLeverDevices;
    }

    /**
     * 获取其他设备列表
     *
     * @return
     */
    public ArrayList<String> getOtherDevices() {
        if (otherDevices == null) {
            otherDevices = new ArrayList<String>();
        } else {
            otherDevices.clear();
        }
        for (int i = 0; i < deivices.size(); i++) {
            int functype = SystemConstant.deviceBeanHashMap.get(deivices.get(i)).getFunctype();
            if (functype != DeviceConstant.FUNCTYPE_PH && functype != DeviceConstant.FUNCTYPE_TEMPERATURE) {
                otherDevices.add(deivices.get(i));
            }
        }
        return otherDevices;
    }

    public ArrayList<String> getSwitchDevices() {
        if (switchDevices == null) {
            switchDevices = new ArrayList<String>();
        } else {
            switchDevices.clear();
        }
        for (int i = 0; i < deivices.size(); i++) {
            int functype = SystemConstant.deviceBeanHashMap.get(deivices.get(i)).getFunctype();
            String label = SystemConstant.deviceBeanHashMap.get(deivices.get(i)).getLabel();
            if (functype == DeviceConstant.FUNCTYPE_SWITCH && label.equals("Switch")) {
                switchDevices.add(deivices.get(i));
            }
        }
        return switchDevices;
    }

    /**
     * 是否存在水位设备
     *
     * @return
     */
    public boolean isWaterLeverExist() {
        boolean isExist = false;
        for (int i = 0; i < deivices.size(); i++) {
            DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(deivices.get(i));
            int functype = deviceBean.getFunctype();
            if (functype == DeviceConstant.FUNCTYPE_WATERLEVER) {
                waterLever = deviceBean;
                isExist = true;
                break;
            }
        }
        return isExist;
    }

    /**
     * 是否存在照明插座设备
     * @return
     */
    public boolean isLightSwitchExist() {
        boolean isExist = false;
        for (int i = 0; i < deivices.size(); i++) {
            DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(deivices.get(i));
            int functype = deviceBean.getFunctype();
            String label = deviceBean.getLabel();
            String userTag = deviceBean.getUserTag();
            if (functype == DeviceConstant.FUNCTYPE_SWITCH) {
                if (label.equals("Light Switch")) {
                    isExist = true;
                    break;
                }
                if ("Light Switch".equals(userTag)) {
                    isExist = true;
                    break;
                }
            }
        }
        return isExist;
    }

    /**
     * 是否存在喂食插座设备
     * @return
     */
    public boolean isFeedSwitchExist() {
        boolean isExist = false;
        for (int i = 0; i < deivices.size(); i++) {
            DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(deivices.get(i));
            int functype = deviceBean.getFunctype();
            String label = deviceBean.getLabel();
            String userTag = deviceBean.getUserTag();
            if (functype == DeviceConstant.FUNCTYPE_SWITCH) {
                if (label.equals("Feed Switch")) {
                    isExist = true;
                    break;
                }
                if ("Feed Switch".equals(userTag)) {
                    isExist = true;
                    break;
                }
            }
        }
        return isExist;
    }

    /**
     * 是否存在换水插座设备
     * @return
     */
    public boolean isWaterSwitchExist() {
        boolean isExist = false;
        for (int i = 0; i < deivices.size(); i++) {
            DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(deivices.get(i));
            int functype = deviceBean.getFunctype();
            String label = deviceBean.getLabel();
            String userTag = deviceBean.getUserTag();
            if (functype == DeviceConstant.FUNCTYPE_SWITCH) {
                if (label.equals("Water Switch")) {
                    isExist = true;
                    break;
                }
                if ("Water Switch".equals(userTag)) {
                    isExist = true;
                    break;
                }
            }
        }
        return isExist;
    }

    /**
     * 是否存在换气插座设备
     * @return
     */
    public boolean isO2SwitchExist() {
        boolean isExist = false;
        for (int i = 0; i < deivices.size(); i++) {
            DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(deivices.get(i));
            int functype = deviceBean.getFunctype();
            String label = deviceBean.getLabel();
            String userTag = deviceBean.getUserTag();
            if (functype == DeviceConstant.FUNCTYPE_SWITCH) {
                if (label.equals("O2 Switch")) {
                    isExist = true;
                    break;
                }
                if ("O2 Switch".equals(userTag)) {
                    isExist = true;
                    break;
                }
            }
        }
        return isExist;
    }

    /**
     * 获得可执行设备列表
     * @return
     */
    public ArrayList<String> getActionDevices() {
        if (actionDevices == null) {
            actionDevices = new ArrayList<String>();
        } else {
            actionDevices.clear();
        }
        for (int i = 0; i < deivices.size(); i++) {
            int functype = SystemConstant.deviceBeanHashMap.get(deivices.get(i)).getFunctype();
            if (functype == DeviceConstant.FUNCTYPE_SWITCH || functype == DeviceConstant.FUNCTYPE_FEEDER) {
                actionDevices.add(deivices.get(i));
            }
        }
        return actionDevices;
    }


    public ArrayList<RuleBean> getRuleList() {
        if (ruleList == null)
            ruleList = new ArrayList<>();
        return ruleList;
    }

    public void setRuleList(ArrayList<RuleBean> ruleList) {
        this.ruleList = ruleList;
    }

    public ArrayList<SceneBean> getSceneList() {
        if (sceneList == null)
            sceneList = new ArrayList<>();
        return sceneList;
    }

    public void setSceneList(ArrayList<SceneBean> sceneList) {
        this.sceneList = sceneList;
    }

    public ArrayList<ScheduleBean> getScheduleList() {
        if (scheduleList == null)
            scheduleList = new ArrayList<>();
        return scheduleList;
    }

    public void setScheduleList(ArrayList<ScheduleBean> scheduleList) {
        this.scheduleList = scheduleList;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.id);
        dest.writeString(this.title);
        dest.writeInt(this.order);
        dest.writeStringList(this.deivices);
        dest.writeString(this.icon_type);
        dest.writeString(this.icon_data);
        dest.writeByte(isAlarm ? (byte) 1 : (byte) 0);
        dest.writeStringList(this.temperatureDevices);
        dest.writeStringList(this.phDevices);
        dest.writeStringList(this.waterLeverDevices);
        dest.writeStringList(this.otherDevices);
        dest.writeStringList(this.switchDevices);
        dest.writeParcelable(this.waterLever, 0);
        dest.writeStringList(this.actionDevices);
        dest.writeList(this.ruleList);
        dest.writeTypedList(sceneList);
        dest.writeList(this.scheduleList);
        dest.writeInt(maintainMode);
    }
}
